<?php
/*
 * 加盟商管理控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ShopManageController extends AdminController {
	/*
	 * 加盟商列表
	 * 
	 */	
	 public function index(){
        $page=I('get.page','1','strip_tags');
        ////获取该运营商下的所有运营商
        $yid=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
        $rank=M('yunying_user')->where("yid='%d'",$yid)->getField("rank");///获取运营商等级
       if($rank==3)
        {
        $where['founder']=$yid;///县级运营商只能看到自己的加盟商

        }
        if($rank==2)
        {///运营商为市
            $pid=M('yunying_user')->where("pid='%d'",$yid)->getField("yid",true);
            if(!empty($pid))
            {
                   $yid.=',';
                   $yid.=implode(",", array_values($pid));
                   $where['founder']=array("in",$yid);
            }else{
                $where['founder']=$yid;
            }
        }
        if($rank==1)
        {////运营商为省级运营商
            $shiid=M('yunying_user')->where("pid='%d'",$yid)->getField("yid",true);///获取所有的市级id
            if(!empty($shiid))
            {
               $yid.=',';
               $yid.=implode(",", array_values($shiid));
               $m['pid']=array("in",$shiid);
             $xianid=M('yunying_user')->where($m)->getField("yid",true);///获取县id

             if(!empty($xianid))
             {
               $yid.=',';
               $yid.=implode("," ,$xianid);
               $where['founder']=array("in",$yid);
             }else{
                $where['founder']=array("in",$yid);

             }

            }else
            {
                $where['founder']=$yid;

            }
        
        }

        $count=M('shop_user')->where($where)->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
        $result=M('shop_user')->where($where)
                         ->limit($pagearr['start'],$pagearr['pagesize'])
                         ->order('shopid desc')->select();

                     

                         
        $this->assign('shops',$result);
        $this->assign('pagearr',$pagearr);
        $this->display('index');
    }
	/*
	 ×添加加盟商
	 */
	public function addShop(){
		////获取当前该运营商的等级
		$rank=$_SESSION['rank'];
	
		$where['a.yid']=$_SESSION['yid'];
		if($rank==1){
			///该运营商为省级运营商(可以添加市级或者县级代理)
			$sheng=M('yunying_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$map['parent_id']=array_keys($sheng);

		   $city=M('area')->where($map)->getField("region_id,region_name");
		}elseif($rank==2) {
			////市级代理(可以添加省级代理)
			$sheng=M('yunying_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$shi=M('yunying_user a')->join("cart_area b on a.shi_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
		    
		    $areaall=M('area')->where("area")->where("parent_id='%d'",array_keys($shi))->getField("region_id,region_name");
		}else{
			////为省级代理商(地址自填);
			$area=M('yunying_user a')->join("cart_area b on a.area_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
             $sheng=M('yunying_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$shi=M('yunying_user a')->join("cart_area b on a.shi_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
		}

        
          
		$this->assign('sheng',$sheng);
		$this->assign('city',$city);
		$this->assign('shi',$shi);
		$this->assign('area',$area)->assign('all',$areaall);
		$this->assign('rank',$rank);
		$this->display();
	}

	/*
	 *保存加盟商
	 */
	public function saveShop(){
         $where['yid']=$_SESSION['yid'];
		$rank=$_SESSION['rank'];
		if($rank==3){

			
			$data['sheng_id']=M('yunying_user')->where($where)->getField("sheng_id");
			$data['shi_id']=M('yunying_user')->where($where)->getField("shi_id");
			$data['area_id']=M('yunying_user')->where($where)->getField("area_id");


		}elseif($rank==2){
			$data['sheng_id']=M('yunying_user')->where($where)->getField("sheng_id");
			$data['shi_id']=M('yunying_user')->where($where)->getField("shi_id");
			

			$data['area_id']=I('post.area_id','','strip_tags');

		}else{
			$data['sheng_id']=M('yunying_user')->where($where)->getField("sheng_id");
			$data['shi_id']=I('post.shi_id','','strip_tags');
			$data['area_id']=I('post.area_id','','strip_tags');

		}
 

		//接收参数
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['connectname']=I('post.connectname','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['username']=I('post.username','','strip_tags');
		$data['showname']=I('post.showname','','strip_tags');
		$data['umobile']=I('post.umobile','','strip_tags');
		$data['uemail']=I('post.uemail','','strip_tags');
		$data['uaddress']=I('post.uaddress','','strip_tags');
		$password=I('post.password','','strip_tags');
		$data['password']=md5($password);
		$data['createtime']=time();
		$data['founder']=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
		$data['istown']=I('post.istown',0,'strip_tags');///是否为乡镇

	
		//保存用户
		$shop_user=M('shop_user');

		
	   $shopid=$shop_user->add($data);///基本信息到加盟商表

	  
	   
	   if($shopid>0 )
	   {
	   
	   	echo "<script type='text/javascript'>alert('添加成功'); window.location.href='./index';</script>";
	   }else
	   {
	   	 
	   	echo "<script type='text/javascript'>alert('添加失败'); window.history.go(-1);window.location.reload();</script>";

	   }
	}
	/*
	 ×编辑加盟商
	 @param shopid 商家id
	 */
	public function editShop(){
			$shopid=I('get.shopid','','strip_tags');
			//获取此商家的信息
			$shop_user=M('shop_user');
			$shop=$shop_user->where('shopid=%d AND isdel=%d',$shopid,0)->select();
            $where['shopid']=$shopid;
			$rank=$_SESSION['rank'];
			if($rank==1){
			///该运营商为省级运营商(可以添加市级或者县级代理)
			$sheng=M('shop_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$defaultshi=M('shop_user a')->join("cart_area b on a.shi_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");////默认选择的市
			$defarea=M('shop_user a')->join("cart_area b on a.area_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");///默认选择的区域
			$map['parent_id']=array_keys($sheng);
		   $city=M('area')->where($map)->getField("region_id,region_name");
		}elseif($rank==2) {
			////市级代理(可以添加省级代理)
			$sheng=M('shop_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$shi=M('shop_user a')->join("cart_area b on a.shi_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
		    $areaall=M('area')->where("area")->where("parent_id='%d'",array_keys($shi))->getField("region_id,region_name");
		    $defaultarea=M('shop_user a')->join("cart_area b on a.area_id=b.region_id")->where("a.shopid='%d'",$shopid)->getField("b.region_id,b.region_name");///默认选中的区域
		}else{
			////为省级代理商(地址自填);
			$area=M('shop_user a')->join("cart_area b on a.area_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
             $sheng=M('shop_user a')->join("cart_area b on a.sheng_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
			$shi=M('shop_user a')->join("cart_area b on a.shi_id=b.region_id")->where($where)->getField("b.region_id,b.region_name");
		}


          
		$this->assign('sheng',$sheng);
		$this->assign('city',$city);
		$this->assign('shi',$shi);
		$this->assign('area',$area)->assign('all',$areaall)->assign('de',$defaultarea)->assign('def',$defaultshi)->assign('defa',$defarea);
		$this->assign('shop',$shop[0]);
		$this->assign('rank',$rank);
		$this->display();
	}
	/*
	 *保存修改后的加盟商
	 *@param shopid 商家id
	 */
	public function updateShop(){
		$rank=$_SESSION['rank'];
		if($rank==3){
			$data['area_id']=M('yunying_user a')->join('cart_area b on a.area_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
			$data['shi_id']=M('yunying_user a')->join('cart_area b on a.shi_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
			$data['sheng_id']=M('yunying_user a')->join('cart_area b on a.sheng_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
		}
		if($rank==2){
			$data['shi_id']=M('yunying_user a')->join('cart_area b on a.shi_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
			$data['sheng_id']=M('yunying_user a')->join('cart_area b on a.sheng_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
			$data['area_id']=I('post.area_id',0,'strip_tags');

		}
		if($rank==1){
			$data['sheng_id']=M('yunying_user a')->join('cart_area b on a.sheng_id=b.region_id')->where("yid='%d'",$_SESSION['yid'])->getField("b.region_id");
			$data['area_id']=I('post.area_id',0,'strip_tags');
			$data['shi_id']=I('post.shi_id',0,'strip_tags');

		}

		//接受参数
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['connectname']=I('post.connectname','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['username']=I('post.username','','strip_tags');
		$data['showname']=I('post.showname','','strip_tags');
		$data['umobile']=I('post.umobile','','strip_tags');
		$data['uemail']=I('post.uemail','','strip_tags');
		$data['uaddress']=I('post.uaddress','','strip_tags');
		$data['upassword']=I('post.upassword','','strip_tags');
		$data['createtime']=time();
		$data['founder']=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
		///$data['urole']=I('post.urole','','strip_tags');
		$data['cateid']=I('post.cateid','','strip_tags');//企业类型
	     $data['istown']=I('post.istown',0,'strip_tags');
		$where['shopid']=I('post.shopid','','strip_tags');
		
		//判断密码是否修改过
		$shop_user=M('shop_user');
		$devide=M('device');
	$shoppassword= $shop_user->where("shopid='%d'",$_GET['shopid'])->getField('password');
		if( !empty($data['upassword'])){
			$data['password']=md5($data['upassword']);
		}
         
         $result=$shop_user->where('shopid=%d',$where['shopid'])->save($data);
       
         if($result!==false )
         {
         	
         	$this->redirect("ShopManage/index");

         }else
         {
         	
         	echo "<script>alert('活动删除成功');location.href='/index.php/Admin/ShopManage/editShop'</script>";

         }

		
		
	} 


	/**
	 * 检测设备的唯一
	 */
	public function unique(){

		$where['devicecode']=$_GET['devicecode'];
		$code=M('device')->where($where)->getField("shopid");
		if(empty($code)){
			$data = array('status'=>200,'code'=>'error','info'=>'可用的设备号');
 			echo json_encode($data);
		}else{
			$data = array('status'=>404,'code'=>'error','info'=>'该设备标识已经使用');
 			echo json_encode($data);

		}
		

	}


	/**
	 * 加盟商搜索
	 */
	public function searchShop(){
		$arg=array();
		$arg['keywords']=I('post.keywords','','strip_tags');
		$shop=D('ShopUser');
		$shops=$shop->search($arg);
		$result=array();
		$result['status']=!empty($shops)?1:0;
		$result['shops']=array_values($shops);
		///var_dump($result);die;

		echo json_encode($result);
		return '';

	}
	/**
	 * 获取区域
	 */
	public function area(){
		 $where['parent_id']=I('get.region','','strip_tags');
        $result=M('area')->where($where)->getField("region_id,region_name");
        $str='';
        foreach($result as $k=>$v){
          $str.= '<option value='.$k.'>'.$v.'</option>';

        }
        echo $str;


	}
}